﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using SystemDiagramNew.SystemDiagramLegend.DataDefine;

namespace SystemDiagramNew.SystemDiagramLegend
{
	public partial class LegendMappingForm : System.Windows.Forms.Form
	{
		public LegendMappingForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
		}

		private void LegendMappingForm_Load(object sender, EventArgs e)
		{
			this.ReadMappingFile();
			this.ReadLegendData();
			this.ReadFamilyData();
			this.InitializeLegendList();
			this.InitializeFamilyList();
		}

		private void LegendMappingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.WriteMappingFile();
		}

		private void ReadFamilyData()
		{
			this.ReadFamilyDataByType("Accessory");
			this.ReadFamilyDataByType("EquipmentSanitary");
			this.ReadFamilyDataForWaterTank("WaterTank");
			this.ReadFamilyDataByType("Hydrant");
			this.ReadFamilyDataByType("PipeValve");
			this.ReadFamilyDataByType("Sprinkler");
		}

		private void ReadFamilyDataByType(string typeName)
		{
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			foreach (ListItem listItem in spaceFamilyDataReader.GetAllNameAndImage(typeName))
			{
				string familyName = "";
				Image preview = null;
				foreach (ListItem listItem2 in spaceFamilyDataReader.GetInfoById(typeName, int.Parse(listItem.Tag.ToString())))
				{
					if (listItem2.Name == "Image")
					{
						if (File.Exists(listItem2.Value))
						{
							preview = new Bitmap(listItem2.Value);
						}
					}
					else if (listItem2.Name == "Family")
					{
						familyName = this.GetFamilyName(listItem2.Value);
					}
				}
				int num = this.m_MappingDataList.FindIndex((MappingData s) => s.FamilyName.CompareTo(familyName) == 0);
				string legendName = "";
				if (num != -1)
				{
					legendName = this.m_MappingDataList[num].LegendName;
				}
				LegendMappingForm.FamilyData item = new LegendMappingForm.FamilyData(familyName, preview, legendName);
				this.m_FamilyDataList.Add(item);
			}
		}

		private void ReadFamilyDataForWaterTank(string typeName)
		{
			foreach (ListItemWaterTank listItemWaterTank in new SpaceFamilyDataReader().GetAllWaterTank(typeName))
			{
				string familyName = "";
				Image preview = new Bitmap(listItemWaterTank.image);
				familyName = this.GetFamilyName(listItemWaterTank.name);
				int num = this.m_MappingDataList.FindIndex((MappingData s) => s.FamilyName.CompareTo(familyName) == 0);
				string legendName = "";
				if (num != -1)
				{
					legendName = this.m_MappingDataList[num].LegendName;
				}
				LegendMappingForm.FamilyData item = new LegendMappingForm.FamilyData(familyName, preview, legendName);
				this.m_FamilyDataList.Add(item);
			}
		}

		private void ReadLegendData()
		{
			string fileName = LegendInfoFilePathKit.LegendDataFilePath();
			this.m_LegendGroupList = XMLKit.ReadLegendXMLFile(fileName);
		}

		private void ReadMappingFile()
		{
			string fileName = LegendInfoFilePathKit.LegendMappingInfoFilePath();
			this.m_MappingDataList = XMLKit.ReadMappingFile(fileName);
		}

		private void WriteMappingFile()
		{
			string text = Product.WorkingLocation + "mappingWithFamily2ndLegend.xml";
			if (!File.Exists(text))
			{
				string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\mappingWithFamily2ndLegend.xml";
				this.CopyFile(sourceFileName, text);
			}
			XMLKit.WriteMappingFile(text, this.m_MappingDataList);
		}

		private void CopyFile(string sourceFileName, string destFileName)
		{
			try
			{
				File.Copy(sourceFileName, destFileName);
			}
			catch (Exception)
			{
			}
		}

		private void InitializeFamilyList()
		{
			this.treeViewFamily.ImageList = this.imglstMapped;
			TreeNode treeNode = new TreeNode("给排水族");
			treeNode.ImageIndex = 2;
			treeNode.SelectedImageIndex = 2;
			this.InitializeFamilyListByType(treeNode, "Accessory", "管道附件");
			this.InitializeFamilyListByType(treeNode, "EquipmentSanitary", "卫生设备");
			this.InitializeFamilyListForWaterTank(treeNode, "WaterTank", "水箱");
			this.InitializeFamilyListByType(treeNode, "Hydrant", "消火栓");
			this.InitializeFamilyListByType(treeNode, "PipeValve", "阀件");
			this.InitializeFamilyListByType(treeNode, "Sprinkler", "喷头");
			this.treeViewFamily.Nodes.Add(treeNode);
			treeNode.Expand();
		}

		private void InitializeFamilyListByType(TreeNode treeRootNode, string typeName, string subName)
		{
			TreeNode treeNode = new TreeNode(subName);
			treeNode.ImageIndex = 2;
			treeNode.SelectedImageIndex = 2;
			treeRootNode.Nodes.Add(treeNode);
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			foreach (ListItem item in spaceFamilyDataReader.GetAllNameAndImage(typeName))
			{
				try
				{
					this.CreateSubChildNode(treeNode, spaceFamilyDataReader, typeName, item);
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
		}

		private void InitializeFamilyListForWaterTank(TreeNode treeRootNode, string typeName, string subName)
		{
			TreeNode treeNode = new TreeNode(subName);
			treeNode.ImageIndex = 2;
			treeNode.SelectedImageIndex = 2;
			treeRootNode.Nodes.Add(treeNode);
			foreach (ListItemWaterTank listItemWaterTank in new SpaceFamilyDataReader().GetAllWaterTank(typeName))
			{
				string familyName = "";
				new Bitmap(listItemWaterTank.image);
				familyName = this.GetFamilyName(listItemWaterTank.name);
				int num = this.m_FamilyDataList.FindIndex((LegendMappingForm.FamilyData s) => s.Name.CompareTo(familyName) == 0);
				if (num == -1)
				{
					break;
				}
				int num2 = 0;
				if (this.m_FamilyDataList[num].LegendName.CompareTo("") != 0)
				{
					num2 = 1;
				}
				TreeNode treeNode2 = new TreeNode(familyName);
				treeNode2.ImageIndex = num2;
				treeNode2.SelectedImageIndex = num2;
				treeNode.Nodes.Add(treeNode2);
			}
		}

		private void CreateSubChildNode(TreeNode treeNode, SpaceFamilyDataReader sfdr, string typeName, ListItem item)
		{
			string familyName = "";
			foreach (ListItem listItem in sfdr.GetInfoById(typeName, int.Parse(item.Tag.ToString())))
			{
				if (listItem.Name == "Image")
				{
					if (File.Exists(listItem.Value))
					{
						new Bitmap(listItem.Value);
					}
				}
				else if (listItem.Name == "Family")
				{
					familyName = this.GetFamilyName(listItem.Value);
				}
			}
			int num = this.m_FamilyDataList.FindIndex((LegendMappingForm.FamilyData s) => s.Name.CompareTo(familyName) == 0);
			if (num == -1)
			{
				return;
			}
			int num2 = 0;
			if (this.m_FamilyDataList[num].LegendName.CompareTo("") != 0)
			{
				num2 = 1;
			}
			TreeNode treeNode2 = new TreeNode(familyName);
			treeNode2.ImageIndex = num2;
			treeNode2.SelectedImageIndex = num2;
			treeNode.Nodes.Add(treeNode2);
		}

		private string GetFamilyName(string fullName)
		{
			int num = fullName.LastIndexOf("\\");
			if (num == -1)
			{
				return fullName;
			}
			return fullName.Substring(num + 1, fullName.Length - num - 5);
		}

		private void InitializeLegendList()
		{
			this.m_LegendPreviewer = new Previewer(this, this.pnlLegend.Left, this.pnlLegend.Top, this.pnlLegend.Width, this.pnlLegend.Height, 1u);
			base.MouseWheel += this.m_LegendPreviewer.MouseWheel;
			this.m_LegendPreviewer90 = new Previewer(this, this.pnlLegend90.Left, this.pnlLegend90.Top, this.pnlLegend90.Width, this.pnlLegend90.Height, 2u);
			base.MouseWheel += this.m_LegendPreviewer90.MouseWheel;
			this.m_LegendPreviewer180 = new Previewer(this, this.pnlLegend180.Left, this.pnlLegend180.Top, this.pnlLegend180.Width, this.pnlLegend180.Height, 3u);
			base.MouseWheel += this.m_LegendPreviewer180.MouseWheel;
			this.m_LegendPreviewer270 = new Previewer(this, this.pnlLegend270.Left, this.pnlLegend270.Top, this.pnlLegend270.Width, this.pnlLegend270.Height, 4u);
			base.MouseWheel += this.m_LegendPreviewer270.MouseWheel;
			List<List<LegendGroup>> list = new List<List<LegendGroup>>();
			foreach (LegendGroup legendGroup in this.m_LegendGroupList)
			{
				bool flag = false;
				for (int i = 0; i < list.Count; i++)
				{
					List<LegendGroup> list2 = list[i];
					if (list2.Count != 0 && list2[0].TheLegendKind == legendGroup.TheLegendKind)
					{
						list[i].Add(legendGroup);
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					list.Add(new List<LegendGroup>
					{
						legendGroup
					});
				}
			}
			foreach (List<LegendGroup> list3 in list)
			{
				LegendGroup legendGroup2 = list3[0];
				TreeNode treeNode = new TreeNode(this.GetLegendKindName(legendGroup2));
				this.treeViewLegend.Nodes.Add(treeNode);
				foreach (LegendGroup legendGroup3 in list3)
				{
					TreeNode node = new TreeNode(legendGroup3.TheName);
					treeNode.Nodes.Add(node);
				}
			}
		}

		private string GetLegendKindName(LegendGroup legendGroup)
		{
			string result = "";
			switch (legendGroup.TheLegendKind)
			{
			case LegendKind.LK_Water:
				result = "给水";
				break;
			case LegendKind.LK_ColdWater:
				result = "冷水";
				break;
			case LegendKind.LK_HotWater:
				result = "热水";
				break;
			case LegendKind.LK_Drain:
				result = "排水";
				break;
			case LegendKind.LK_Valves:
				result = "阀件";
				break;
			case LegendKind.LK_ApplianceText:
				result = "器具文字";
				break;
			case LegendKind.LK_ArchSymbols:
				result = "建筑符号";
				break;
			case LegendKind.LK_SanitaryWare:
				result = "卫生器具";
				break;
			case LegendKind.LK_Hydrant:
				result = "消火栓";
				break;
			case LegendKind.LK_SprinklerHead:
				result = "喷头";
				break;
			}
			return result;
		}

		private void treeViewFamily_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.treeViewFamilyAfterSelect();
		}

		private void treeViewFamilyAfterSelect()
		{
			if (!this.SelectedFamilyNode())
			{
				return;
			}
			this.treeViewFamily.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.treeViewFamily.SelectedNode.ForeColor = System.Drawing.Color.White;
			int index = this.FindSelectedFamilyIndex();
			LegendMappingForm.FamilyData familyData = this.m_FamilyDataList[index];
			Image preview = familyData.Preview;
			this.tbName.Text = familyData.Name;
			this.pbFamily.Image = AssistFunc.ResizeImage((Bitmap)preview, this.pbFamily.Width, this.pbFamily.Height);
			if (familyData.LegendName.CompareTo("") == 0)
			{
				this.lbMapping.Text = "";
				this.treeViewLegend.SelectedNode = null;
			}
			else
			{
				int index2 = this.m_LegendGroupList.FindIndex((LegendGroup s) => s.TheName.CompareTo(familyData.LegendName) == 0);
				this.legendGroup = this.m_LegendGroupList[index2];
				this.lbMapping.Text = this.legendGroup.TheName;
				this.treeViewLegend.SelectedNode = this.FindTreeViewLegendNodeByName(this.legendGroup.TheName);
			}
			this.Refresh();
		}

		private void treeViewLegend_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.Refresh();
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				List<CurveInfo> curves = new List<CurveInfo>();
				List<TextNoteInfo> textNotes = new List<TextNoteInfo>();
				List<FillRegion> fillRegions = new List<FillRegion>();
				int num = this.FindSelectedLegendIndex();
				if (m.WParam.ToInt32() == 1)
				{
					if (num < 0)
					{
						this.m_LegendPreviewer.Curves = curves;
						this.m_LegendPreviewer.TextNotes = textNotes;
						this.m_LegendPreviewer.FillRegions = fillRegions;
						return;
					}
					LegendGroup legendGroup = this.m_LegendGroupList[num];
					this.m_LegendPreviewer.Curves = legendGroup.RightLink.CurveInfoList;
					this.m_LegendPreviewer.TextNotes = legendGroup.RightLink.TextNoteInfoList;
					this.m_LegendPreviewer.FillRegions = legendGroup.RightLink.FillRegionList;
					this.m_LegendPreviewer.DrawReferenceBox = true;
					return;
				}
				else if (m.WParam.ToInt32() == 2)
				{
					if (num < 0)
					{
						this.m_LegendPreviewer90.Curves = curves;
						this.m_LegendPreviewer90.TextNotes = textNotes;
						this.m_LegendPreviewer90.FillRegions = fillRegions;
						return;
					}
					LegendGroup legendGroup2 = this.m_LegendGroupList[num];
					this.m_LegendPreviewer90.Curves = legendGroup2.TopLink.CurveInfoList;
					this.m_LegendPreviewer90.TextNotes = legendGroup2.TopLink.TextNoteInfoList;
					this.m_LegendPreviewer90.FillRegions = legendGroup2.TopLink.FillRegionList;
					this.m_LegendPreviewer90.DrawReferenceBox = true;
					return;
				}
				else if (m.WParam.ToInt32() == 3)
				{
					if (num < 0)
					{
						this.m_LegendPreviewer180.Curves = curves;
						this.m_LegendPreviewer180.TextNotes = textNotes;
						this.m_LegendPreviewer180.FillRegions = fillRegions;
						return;
					}
					LegendGroup legendGroup3 = this.m_LegendGroupList[num];
					this.m_LegendPreviewer180.Curves = legendGroup3.LeftLink.CurveInfoList;
					this.m_LegendPreviewer180.TextNotes = legendGroup3.LeftLink.TextNoteInfoList;
					this.m_LegendPreviewer180.FillRegions = legendGroup3.LeftLink.FillRegionList;
					this.m_LegendPreviewer180.DrawReferenceBox = true;
					return;
				}
				else if (m.WParam.ToInt32() == 4)
				{
					if (num < 0)
					{
						this.m_LegendPreviewer270.Curves = curves;
						this.m_LegendPreviewer270.TextNotes = textNotes;
						this.m_LegendPreviewer270.FillRegions = fillRegions;
						return;
					}
					LegendGroup legendGroup4 = this.m_LegendGroupList[num];
					this.m_LegendPreviewer270.Curves = legendGroup4.BottomLink.CurveInfoList;
					this.m_LegendPreviewer270.TextNotes = legendGroup4.BottomLink.TextNoteInfoList;
					this.m_LegendPreviewer270.FillRegions = legendGroup4.BottomLink.FillRegionList;
					this.m_LegendPreviewer270.DrawReferenceBox = true;
					return;
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}

		private bool SelectedLegendNode()
		{
			TreeNode selectedNode = this.treeViewLegend.SelectedNode;
			return selectedNode != null && selectedNode.FirstNode == null;
		}

		private int FindSelectedLegendIndex()
		{
			if (!this.SelectedLegendNode())
			{
				return -1;
			}
			string nodeText = this.treeViewLegend.SelectedNode.Text;
			return this.m_LegendGroupList.FindIndex((LegendGroup s) => s.TheName.CompareTo(nodeText) == 0);
		}

		private TreeNode FindTreeViewLegendNodeByName(string legendName)
		{
			foreach (object obj in this.treeViewLegend.Nodes)
			{
				TreeNode tnParent = (TreeNode)obj;
				TreeNode treeNode = this.FindNode(tnParent, legendName);
				if (treeNode != null)
				{
					return treeNode;
				}
			}
			return null;
		}

		private TreeNode FindNode(TreeNode tnParent, string strValue)
		{
			if (tnParent == null)
			{
				return null;
			}
			if (tnParent.Text == strValue)
			{
				return tnParent;
			}
			TreeNode treeNode = null;
			foreach (object obj in tnParent.Nodes)
			{
				TreeNode tnParent2 = (TreeNode)obj;
				treeNode = this.FindNode(tnParent2, strValue);
				if (treeNode != null)
				{
					break;
				}
			}
			return treeNode;
		}

		private bool SelectedFamilyNode()
		{
			TreeNode selectedNode = this.treeViewFamily.SelectedNode;
			return selectedNode != null && selectedNode.FirstNode == null;
		}

		private int FindSelectedFamilyIndex()
		{
			if (!this.SelectedFamilyNode())
			{
				return -1;
			}
			string nodeText = this.treeViewFamily.SelectedNode.Text;
			return this.m_FamilyDataList.FindIndex((LegendMappingForm.FamilyData s) => s.Name.CompareTo(nodeText) == 0);
		}

		private void btnSearchLegend_Click(object sender, EventArgs e)
		{
			string text = this.tbSearchLegend.Text;
			List<TreeNode> list = new List<TreeNode>();
			foreach (object obj in this.treeViewLegend.Nodes)
			{
				TreeNode tnParent = (TreeNode)obj;
				this.SearchNode(tnParent, text, ref list);
			}
			if (list.Count == 0)
			{
				MessageBox.Show("没有找到相关的查找项", "盈建科提示");
				return;
			}
			if (list.Count == 1)
			{
				this.treeViewLegend.SelectedNode = list[0];
				return;
			}
			ListSearchLegendForm listSearchLegendForm = new ListSearchLegendForm(list);
			if (listSearchLegendForm.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			this.treeViewLegend.SelectedNode = listSearchLegendForm.SelectNode;
		}

		private void SearchNode(TreeNode tnParent, string strValue, ref List<TreeNode> findedNodeList)
		{
			if (tnParent == null)
			{
				return;
			}
			if (tnParent.Text.IndexOf(strValue) != -1)
			{
				findedNodeList.Add(tnParent);
			}
			foreach (object obj in tnParent.Nodes)
			{
				TreeNode tnParent2 = (TreeNode)obj;
				this.SearchNode(tnParent2, strValue, ref findedNodeList);
			}
		}

		private void btnModify_Click(object sender, EventArgs e)
		{
			base.Size = new Size(650, 573);
			this.btnOk.Location = new System.Drawing.Point(545, 508);
			this.btnModify.Visible = false;
			this.groupBox1.Enabled = false;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.treeViewFamilyAfterSelect();
			base.Size = new Size(450, 573);
			this.btnOk.Location = new System.Drawing.Point(352, 508);
			this.btnModify.Visible = true;
			this.groupBox1.Enabled = true;
		}

		private void treeViewFamily_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.treeViewFamily.SelectedNode != null)
			{
				this.treeViewFamily.SelectedNode.BackColor = System.Drawing.Color.White;
				this.treeViewFamily.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void btnMapping_Click(object sender, EventArgs e)
		{
			if (!this.SelectedFamilyNode() || !this.SelectedLegendNode())
			{
				return;
			}
			int index = this.FindSelectedLegendIndex();
			LegendGroup legendGroup = this.m_LegendGroupList[index];
			this.lbMapping.Text = legendGroup.TheName;
			int index2 = this.FindSelectedFamilyIndex();
			this.m_FamilyDataList[index2].LegendName = legendGroup.TheName;
			LegendMappingForm.FamilyData familyData = this.m_FamilyDataList[index2];
			this.treeViewFamily.SelectedNode.ImageIndex = 1;
			this.treeViewFamily.SelectedNode.SelectedImageIndex = 1;
			int num = this.m_MappingDataList.FindIndex((MappingData s) => s.FamilyName.CompareTo(familyData.Name) == 0);
			if (num == -1)
			{
				MappingData item = new MappingData(familyData.Name, legendGroup.TheName);
				this.m_MappingDataList.Add(item);
			}
			else
			{
				this.m_MappingDataList[num].LegendName = legendGroup.TheName;
			}
			this.treeViewFamily.Select();
			base.Size = new Size(450, 573);
			this.btnOk.Location = new System.Drawing.Point(352, 508);
			this.btnModify.Visible = true;
			this.groupBox1.Enabled = true;
		}

		private void treeViewLegend_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
		}

		private readonly List<LegendMappingForm.FamilyData> m_FamilyDataList = new List<LegendMappingForm.FamilyData>();

		private LegendGroup legendGroup;

		private List<LegendGroup> m_LegendGroupList = new List<LegendGroup>();

		private Previewer m_LegendPreviewer;

		private Previewer m_LegendPreviewer180;

		private Previewer m_LegendPreviewer270;

		private Previewer m_LegendPreviewer90;

		private List<MappingData> m_MappingDataList = new List<MappingData>();

		private ExternalCommandData m_Revit;

		public class FamilyData
		{
			public FamilyData(LegendMappingForm.FamilyData orig)
			{
				this.Name = orig.Name;
				this.Preview = orig.Preview;
				this.LegendName = orig.LegendName;
			}

			public FamilyData(string name, Image preview, string legendName)
			{
				this.Name = name;
				this.Preview = preview;
				this.LegendName = legendName;
			}

			public string Name { get; private set; }

			public Image Preview { get; private set; }

			public string LegendName { get; set; }
		}
	}
}
